﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "IFieldProxy.h"
#include "UniIdentity.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

/// 本文件是界面实现的辅助类，不包含GDMP API的演示内容

using namespace gcmp;
using namespace Sample;

std::wstring Sample::IFieldProxy::GetFieldValue()
{
    switch (this->m_fieldType)
    {
    case FieldType::Int:
        return StringUtil::ToWString(m_intValue);
    case FieldType::Double:
        return StringUtil::ToWString(m_doubleValue, 2);
    case FieldType::String:
    default:
        return m_fieldValue;
    }
    return m_fieldValue;
}

void Sample::IFieldProxy::UpdateValue(bool newValue)
{
    if (newValue != m_boolValue)
    {
        m_isUpdated = true;
    }
    m_boolValue = newValue;
}

void Sample::IFieldProxy::UpdateValue(std::wstring newValue)
{
    if (newValue != m_fieldValue)
    {
        m_isUpdated = true;
    }
    m_fieldValue = newValue;
}

void Sample::IFieldProxy::UpdateValue(int newValue)
{
    if (newValue != m_intValue)
    {
        m_isUpdated = true;
    }
    m_intValue = newValue;
}

void Sample::IFieldProxy::UpdateValue(double newValue)
{
    if (!MathUtils::IsEqual(newValue, m_doubleValue))
    {
        m_isUpdated = true;
    }
    m_doubleValue = newValue;
}

void Sample::IFieldProxy::SetSetBoolFieldFunc(SetBoolFieldFunc setFieldFunc)
{
    m_setBoolFieldFunc = setFieldFunc; 
    m_isReadOnly = false;
}

void Sample::IFieldProxy::SetSetBoolFieldFuncBool(SetBoolFieldFuncBool setFieldFuncBool)
{
    m_setBoolFieldFuncBool = setFieldFuncBool;
    m_isReadOnly = false;
}

void Sample::IFieldProxy::SetSetEnumFieldFunc(SetTextOrEnumFieldFunc setEnumFieldFunc)
{
    m_setEnumFieldFunc = setEnumFieldFunc;
    m_isReadOnly = false;
}

void Sample::IFieldProxy::SetSetTextConstRefFieldFuncBool(SetTextConstRefFieldFuncBool setTextConstRefFieldFuncBool)
{
    m_setTextConstRefFieldFuncBool = setTextConstRefFieldFuncBool;
    m_isReadOnly = false;
}

void Sample::IFieldProxy::SetSetIntFieldFunc(SetIntFieldFunc setIntFieldFunc)
{
    m_setIntFieldFunc = setIntFieldFunc;
    m_isReadOnly = false;
}

void Sample::IFieldProxy::SetSetDoubleFieldFunc(SetDoubleFieldFunc setDoubleFieldFunc)
{
    m_setDoubleFieldFunc = setDoubleFieldFunc;
    m_isReadOnly = false;
}

void Sample::IFieldProxy::SetSetDoubleFieldFuncBool(SetDoubleFieldFuncBool setDoubleFieldFuncBool)
{
    m_setDoubleFieldFuncBool = setDoubleFieldFuncBool;
    m_isReadOnly = false;
}

void Sample::IFieldProxy::RunSetFieldFunc()
{
    if (!m_isUpdated)
    {
        return;
    }
    switch (this->m_fieldType)
    {
    case FieldType::Bool:
        if (m_setBoolFieldFunc)
        {
            m_setBoolFieldFunc(m_boolValue);
        }
        if (m_setBoolFieldFuncBool)
        {
            m_setBoolFieldFuncBool(m_boolValue);
        }
        m_isReadOnly = false;
        return;
    case FieldType::Int:
        if (m_intValue)
        {
            m_setIntFieldFunc(m_intValue);
        }
        return;
    case FieldType::Double:
        if (m_setDoubleFieldFunc)
        {
            m_setDoubleFieldFunc(m_doubleValue);
        }
        if (m_setDoubleFieldFuncBool)
        {
            m_setDoubleFieldFuncBool(m_doubleValue);
        }
        return;
    case FieldType::String:
    case FieldType::Enum:
        if (m_setEnumFieldFunc)
        {
            m_setEnumFieldFunc(m_fieldValue);
        }
        if (m_setTextConstRefFieldFuncBool)
        {
            m_setTextConstRefFieldFuncBool(m_fieldValue);
        }
        return;
    default:
        return;
    }
}

